package stock;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;

import com.gargoylesoftware.htmlunit.html.HtmlElement;

import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import reactor.netty.http.client.HttpClient;

public class TestCls2 {

	/**
	 * 获取股票概念
	 * 
	 * @param args
	 * @throws InterruptedException
	 */
	public static void main1(String[] args) throws InterruptedException {

		// 创建WebClient实例，指定信任所有SSL/TLS证书的工厂
		String url = "https://www.cls.cn/stock?code=sh605376";

		// 指定Chrome浏览器核心
		System.setProperty("webdriver.chrome.driver", "D:\\chromedriver\\chromedriver.exe");

		// 创建Chrome浏览器驱动
		// 配置Chrome选项
		ChromeOptions options = new ChromeOptions();
		options.addArguments("--headless"); // 无界面运行

		// 创建Chrome浏览器驱动
		WebDriver driver = new ChromeDriver(options);
		driver.get(url);
		// WebDriverWait wait = new WebDriverWait(driver, 2000);
		// driver.manage().timeouts().implicitlyWait(10000, TimeUnit.SECONDS);
		// 抓取网页内容
		// String pageContent = driver.getPageSource();

		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		List<WebElement> elements = driver.findElements(By.xpath("//div[@class='m-b-10']//a"));

//		// 遍历打印每个元素的文本内容
//		for (WebElement element : elements) {
//			System.out.println(element.getText());
//		}

		// 获取标签内容
		for (WebElement element : elements) {
			System.out.println(element.getText());
		}

	}

	public static void main(String[] args) throws InterruptedException {

		// 创建WebClient实例，指定信任所有SSL/TLS证书的工厂
		String url = "https://www.cls.cn/stock?code=sh605376";

		// 指定Chrome浏览器核心
		System.setProperty("webdriver.chrome.driver", "D:\\chromedriver\\chromedriver.exe");

		// 创建Chrome浏览器驱动
		// 配置Chrome选项
		ChromeOptions options = new ChromeOptions();
		// options.addArguments("--headless"); // 无界面运行

		// 创建Chrome浏览器驱动
		WebDriver driver = new ChromeDriver(options);
		driver.get(url);
		// WebDriverWait wait = new WebDriverWait(driver, 2000);
		// driver.manage().timeouts().implicitlyWait(10000, TimeUnit.SECONDS);
		// 抓取网页内容
		// String pageContent = driver.getPageSource();

		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		List<WebElement> webe = driver.findElements(By.xpath("//div[@class='f-s-14 list-more-button more-button']"));

		webe.get(0).click();
		Thread.sleep(1000);

		List<WebElement> elements = driver.findElements(By.xpath("//div[@class='b-c-e6e7ea telegraph-list']//a"));

		// 获取标签内容
		for (WebElement element : elements) {

			String content = element.getText();
			if (content.indexOf("涨停原因") > -1) {
				System.out.println(element.getText());
				int t0 = element.getText().indexOf(":");

				int t1 = element.getText().length();

				System.out.println(element.getText().substring(t0, t1));
			}
			if (content.indexOf("大涨原因") > -1) {
				System.out.println(element.getText());

			}
		}
		List<WebElement> elements2 = driver
				.findElements(By.xpath("//div[@class='p-r m-b-10 quote-text-border']//span"));
		// 执行点击操作
		String shizhi = "0";
		for (int i = 0; i < elements2.size(); i++) {
			String str = elements2.get(i).getText();
			if (str.indexOf("总市值") > -1) {
				shizhi  = elements2.get(i+1).getText().replaceAll("亿", "");
				break;
			}
		}
	}

}
